package com.css.fxfzysx.modules.vaolEngineering.controller;

import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vaolEngineering.param.DeleteParam;
import com.css.fxfzysx.modules.vaolEngineering.param.PageParam;
import com.css.fxfzysx.modules.vaolEngineering.param.UpdateParam;
import com.css.fxfzysx.modules.vaolEngineering.service.ValEngineeringService;
import com.css.fxfzysx.modules.vaolEngineering.vo.LifelineCalculationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("valEngineering")
@Slf4j
public class ValEngineeringController {

    @Autowired
    private ValEngineeringService valEngineeringService;

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("page")
    public RestResponse page(PageParam param, HttpServletRequest request) {
        try {
            HashMap<String,Object> map = valEngineeringService.page(param, request);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get page error:{}",e.getMessage(),e);
            return RestResponse.fail("查看失败!");
        }
    }


    /**
     * 数据版本
     */
    @GetMapping("getVersionByType")
    public RestResponse getVersionByType(Integer type,HttpServletRequest request) {
        try {
            HashMap<String,Object> map = valEngineeringService.getVersionByType(type, request);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get getVersionByType error:{}",e.getMessage(),e);
            return RestResponse.fail("查看失败!");
        }
    }

    /**
     * 详情
     * @param type
     * @param id
     * @return
     */
    @GetMapping("getByIdAndType")
    public RestResponse getByIdAndType(Integer type,String id,Integer bridgeType){
        try {
            LifelineCalculationVo lifelineCalculationVo = valEngineeringService.getByIdAndType(type,id,bridgeType);
            return RestResponse.succeed(lifelineCalculationVo);
        } catch (Exception e) {
            log.error("get getByIdAndType error:{}",e.getMessage(),e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @PostMapping("updateByIdAndType")
    public RestResponse updateByIdAndType(@RequestBody UpdateParam param){
        try {
            valEngineeringService.updateByIdAndType(param);
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            log.error("post updateByIdAndType error:{}",e.getMessage(),e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 重新计算
     * @param type
     * @param id
     * @return
     */
    @GetMapping("recalculate")
    public RestResponse recalculate(Integer type,String id,Integer bridgeType){
        try {
            return valEngineeringService.recalculate(type,id,bridgeType);
        } catch (Exception e) {
            log.error("get recalculate error:{}",e.getMessage(),e);
            return RestResponse.fail("重新计算失败!");
        }
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("deleteByTypeAndId")
    public RestResponse deleteByTypeAndId(@RequestBody List<DeleteParam> params){
        try {
            valEngineeringService.deleteByTypeAndId(params);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("get deleteByTypeAndId error:{}",e.getMessage(),e);
            return RestResponse.fail("删除失败!");
        }
    }
}
